﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace SmartyOcr
{
    /// <summary>
    /// 基本的Ocr实现  
    /// xue8000@gmail.com
    /// xueliang @2012-11-11
    /// </summary>
    public class BasicOcr
    {
        #region//数字特证库
        private static LinkedList<KeyValuePair<string, string>> Charactoristic { set; get; }
        #endregion
        static BasicOcr()
        {
            Charactoristic = SmartyOcr.CharactoristicLoader.Charactoristic;
        }
        /// <summary>
        /// 识别指定图片上的文字
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <returns>返回图片上的文字</returns>
        public static string Recognize(string imagePath)
        {
            var charactors = new StringBuilder();
            var data = ImageReader.ExtractContent(imagePath);
            foreach (var info in data)
            {
                foreach (var kv in Charactoristic)
                {
                    if (info == kv.Value)
                    {
                        charactors.Append(kv.Key);
                        break;
                    }
                }
            }
            return charactors.ToString();
        }
        /// <summary>
        /// 识别指定图片上的文字
        /// </summary>
        /// <param name="uri">图片地址</param>
        /// <returns>返回图片上的文字</returns>
        public static string Recognize(Uri uri) {
            var charactors = new StringBuilder();
            var data = ImageReader.ExtractContent(uri);
            foreach (var info in data)
            {
                foreach (var kv in Charactoristic)
                {
                    if (info == kv.Value)
                    {
                        charactors.Append(kv.Key);
                        break;
                    }
                }
            }
            return charactors.ToString();
        }
    }
}

